package edu.nf.aop.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * @author wangl
 * @date 2021/9/24
 * JDK动态代理工厂
 */
public class JdkAopProxy extends AbstractAopProxy {

    private final Class<?>[] interfaces;

    public JdkAopProxy(Class<?>[] interfaces) {
        this.interfaces = interfaces;
    }

    /**
     * 使用JDK动态代理
     */
    @Override
    @SuppressWarnings("unchecked")
    public <T> T createProxy(Class<T> targetClass) {
        InvocationHandler handler = (proxy, method, args) ->
                invoke(targetClass, method, args);
        return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
                interfaces, handler);
    }
}
